<?php

namespace Home\Controller;
use Think\Controller;
use Home\Controller\BaseController;


class UploadFileController extends BaseController {
	private $path ;

	function _initialize(){
		parent::_initialize();
		$this->path =  "uploads/" .$this->account ."/";
		$this->mkdirAccount();
	}
	
	function index(){


		$this->display();
	}
	
	function upload(){

		$filename = $_POST['name'];
		
		if(!$_FILES){
			echo "上传错误";
			return;
		}

		if ($_FILES["file"]["error"] > 0)
		{
			echo "错误：: " . $_FILES["file"]["error"] . "<br>";
		}
		else
		{
			
			// 如果没有 upload 目录，你需要创建它，upload 目录权限为 777
			if (file_exists($this->path.$filename))
			{
				echo  $filename . " 文件已经存在。请先删除原文件后再次上传 ";
			}
			else
			{
				echo $_POST['name']."上传成功";
				// 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
				move_uploaded_file($_FILES["file"]["tmp_name"], $this->path.$filename);
				
				$data['user_id'] = $this->user_id ;
				$data['filename'] = $filename;
				$data['filepath'] = $this->path.$filename;
				$data['addtime'] = time();
				M('filelist')->add($data);
			
			}
		}

	}

	function mkdirAccount(){
		if(!is_dir($this->path)){
			mkdir($this->path);
		};
	}


   

}